package com.lzh.gulimall.ssoclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @createTime: 2020-06-29 19:44
 **/

@Controller
public class HelloController {

    @Value("${sso.server.url}")
    private String ssoServerUrl;



    /**
     * 无需登录就可访问
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/hello")
    public String hello() {
        return "hello";
    }

    /**
     * 该接口需要登录
     * 感知这次是在ssoserver登录成功，跳回来的
     * @param model
     * @param session
     * @return token 只要去ssoserver登录成功跳回来就会带上
     */
    @GetMapping(value = "/employees")
    public String employees(Model model, HttpSession session, @RequestParam(value = "token",required = false) String token) {

        if (!StringUtils.isEmpty(token)){
            //去session登陆成功跳回来就会带上
            session.setAttribute("loginUser","zhangsan");
        }

        List<String> emps = new ArrayList<>();
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser == null) {
            //没登录,跳转到服务器登录
            //跳转过去以后，使用url上的查询参数标识我们自己是哪个页面
            return "redirect:"+ssoServerUrl+"?redirect=http://client1.com:8081/employees";
        } else {
            emps.add("张三");
            emps.add("李四");
        }
        model.addAttribute("emps",emps);
        return "employees";
    }

}
